﻿using System.Diagnostics;
using System.Management;

public class SystemInfo
{
    public static void GetSystemInfo()
    {
        // 获取计算机名
        string computerName = Environment.MachineName;
        Debug.WriteLine("计算机名: " + computerName);

        // 获取操作系统信息
        string osVersion = Environment.OSVersion.VersionString;
        Debug.WriteLine("操作系统版本: " + osVersion);

        // 获取处理器信息
        string processor = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");
        Debug.WriteLine("处理器名称: " + processor);

        // 获取内存信息
        long totalMemory = Environment.WorkingSet;
        Debug.WriteLine("内存总量: " + totalMemory);

        // 获取网卡信息
        string[] networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
            .Select(i => i.Name).ToArray();
        Debug.WriteLine("网卡信息: " + string.Join(",", networkInterfaces));

        // 获取磁盘信息
        string[] logicalDrives = Environment.GetLogicalDrives();
        Debug.WriteLine("磁盘信息: " + string.Join(",", logicalDrives));

        // 获取IP地址
        string ipAddress = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
        Debug.WriteLine("IP地址: " + ipAddress);
    }

    public static void ShowInfo()
    {
        Console.WriteLine("获取设备信息：");

        Console.WriteLine("IP：" + getIP());
        Console.WriteLine("OSVersion：" + getOSVersion());
        Console.WriteLine("CpuName：" + getCpuName());
        Console.WriteLine("BoardType：" + getBoardType());
        Console.WriteLine("Mac：" + getLocalhostMac());
        Console.WriteLine("PhysicalMemory：" + getPhysicalMemory());
        Console.WriteLine("SizeOfDisk：" + getSizeOfDisk());
        Console.WriteLine("Resolution：");
        getResolution();
    }

    #region 旧版代码

    public static string getLocalhostName()
    {
        return Environment.GetEnvironmentVariable("COMPUTERNAME");
    }

    public static string getLocalhostMac()
    {
        string mac = null;
        ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection queryCollection = query.Get();
        foreach (ManagementObject mo in queryCollection)
        {
            if (mo["IPEnabled"].ToString() == "True")
                mac = mo["MacAddress"].ToString();
        }
        return (mac);
    }

    public static string getComputerName()
    {
        return Environment.MachineName;
    }

    public static string getResolution()
    {
        ManagementObjectSearcher mydisplayResolution = new ManagementObjectSearcher("SELECT CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
        foreach (ManagementObject record in mydisplayResolution.Get())
        {
            Console.WriteLine("-----------------------Current Resolution---------------------------------");
            Console.WriteLine("CurrentHorizontalResolution  -  " + record["CurrentHorizontalResolution"]);
            Console.WriteLine("CurrentVerticalResolution  -  " + record["CurrentVerticalResolution"]);
        }

        return "";
        //return SystemInformation.PrimaryMonitorMaximizedWindowSize.Width + " x " + SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
    }

    public static string getSystemType()
    {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            st = mo["SystemType"].ToString();
        }
        return st;
    }

    public static string getOSBit()
    {
        if (Environment.Is64BitOperatingSystem)
        {
            return "64bit.";
        }
        else
        {
            return "32bit.";
        }
    }

    public static string getOSVersion()
    {
        return Environment.OSVersion.VersionString;
    }

    public static string getCpuName()
    {
        string st = "";
        ManagementObjectSearcher driveID = new ManagementObjectSearcher("Select * from Win32_Processor");
        foreach (ManagementObject mo in driveID.Get())
        {
            st = mo["Name"].ToString();
        }
        return st;
    }

    public static string getVideoPNPID()
    {
        string st = "";
        ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
        foreach (ManagementObject mo in mos.Get())
        {
            st = mo["PNPDeviceID"].ToString();
        }
        return st;
    }

    public static string getSizeOfDisk()
    {
        ManagementClass mc = new ManagementClass("Win32_DiskDrive");
        ManagementObjectCollection moj = mc.GetInstances();
        foreach (ManagementObject m in moj)
        {
            //return m.Properties["Size"].Value.ToString();
            return (long.Parse(m.Properties["Size"].Value.ToString()) / (1024 * 1024 * 1024)).ToString("N0") + " GB";
        }
        return "-1";
    }

    public static string getBoardType()
    {
        string st = "";
        ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
        foreach (ManagementObject mo in mos.Get())
        {
            st = mo["Product"].ToString();
        }
        return st;
    }

    public static string getPhysicalMemory()
    {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            st = mo["TotalPhysicalMemory"].ToString();
        }
        st = (long.Parse(st) / (1024 * 1024)).ToString("N0") + " MB";
        return st;
    }

    public static string getIP()
    {
        try
        {
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    System.Array ar;
                    ar = (System.Array)(mo.Properties["IpAddress"].Value);
                    st = ar.GetValue(0).ToString();
                    break;
                }
            }
            moc = null;
            mc = null;
            return st;
        }
        catch
        {
            return "unknow";
        }
    }

    #endregion 旧版代码
}